package com.wjpeng.wms.domain;

import com.wjpeng.wms.util.JSONUtil;
import lombok.Getter;
import lombok.Setter;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Setter @Getter
public class Product {
    private Long id;

    private String name;

    private String sn;

    private BigDecimal costPrice;

    private BigDecimal salePrice;

    private String imagePath;

    private String intro;

    private Long brandId;

    private String brandName;

    //fbca9b1b-63b6-4be3-b793-a805bd2b7ff1.jpg
    //fbca9b1b-63b6-4be3-b793-a805bd2b7ff1_small.jpg
    public String getSmallImage(){
        if(StringUtils.hasLength(imagePath)){
            int i = imagePath.lastIndexOf('.');//定义分隔符'.',遇到.就截取
            String filename = imagePath.substring(0, i);//截取字符串fbca9b1b-63b6-4be3-b793-a805bd2b7ff1
            String ext = imagePath.substring(i);//获取后缀名.jsp
            return filename+"_small"+ext;//fbca9b1b-63b6-4be3-b793-a805bd2b7ff1_small.jpg
        }
        return null;
    }

    //把当前对象中的部分信息转成JSON对象
    public String getJsonString(){
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        map.put("name",name);
        map.put("costPrice",costPrice);
        map.put("salePrice",salePrice);
        map.put("brandName",brandName);

        return JSONUtil.toJSONString(map);
    }

}